#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    // 1. 获取输入框内的内容
    QString text_input = ui->lineEdit->text();
    // 2. 将获取内容添加到QListWidget中
    ui->listWidget->addItem(text_input);
}

void Widget::on_pushButton_delete_clicked()
{
    // 1. 获取被选中的元素
    int row = ui->listWidget->currentRow();
    if(row < 0) return;
    // 2. 按行号进行删除
    ui->listWidget->takeItem(row);
}

// 列表项发生变化时 触发
void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if(current)
        ui->label_current->setText("当前选中的元素: " + current->text());
    else
        ui->label_current->setText("当前选中的元素: ");

    if(previous)
        ui->label_previous->setText("此前选中的元素: " + previous->text());
    else
        ui->label_previous->setText("此前选中的元素: ");
}
